package com.example.statistics.ui.market

import com.example.statistics.bean.DefineInputBean
import com.example.statistics.bean.MarketBean
import com.example.statistics.bean.MarketCityBean
import com.example.statistics.bean.PreferenceArea
import com.example.statistics.common.BaseInputActivity
import com.example.statistics.constant.PreferenceConstants
import com.example.statistics.ex.*
import com.example.statistics.net.UrlConfig
import com.example.statistics.utils.AreaUtil
import com.example.statistics.weight.TextProcessingView
import com.google.gson.Gson
import com.jhj.prompt.fragment.OptionsFragment
import com.jhj.prompt.fragment.options.interfaces.OnOptionsSelectedListener

class MarketInfoAddActivity : BaseInputActivity<MarketBean>() {

    private var marketCity: PreferenceArea? = null
    override val title: String
        get() = "新市场"

    override val url: String
        get() = UrlConfig.MARKET_ADD


    override val inputDataList: List<DefineInputBean>
        get() {
            marketCity = preferenceFind(PreferenceArea::class.java, PreferenceConstants.MARKET_CITY)
            extentParams.add("province" to marketCity?.province.orEmpty())
            extentParams.add("city" to marketCity?.city.orEmpty())
            extentParams.add("area" to marketCity?.area.orEmpty())
            return listOf(
                DefineInputBean(
                    "地区：", "city", TextProcessingView.STYLE_SELECTOR_JUMPS,
                    marketCity?.city + marketCity?.area,
                    /*afterTextChangeListener = { str, list ->
                        if (str != list["position"].contentText) {
                            list["position"].contentText = str
                        }
                    },*/
                    clicked = areaDialog(),
                ),
                DefineInputBean(
                    "市场：", "market", TextProcessingView.STYLE_INPUT, isNull = false,
                ),
                DefineInputBean(
                    "位置：", "position", TextProcessingView.STYLE_INPUT,
                    //marketCity?.city?.areaName + marketCity?.area?.areaName,
                ),
                DefineInputBean(
                    "描述：", "description", TextProcessingView.STYLE_INPUT
                ),
            )
        }


    private fun areaDialog(): TextProcessingView.(Map<String, TextProcessingView>) -> Unit {
        return {
            areaList { provinceList, cityList, districtList ->
                OptionsFragment.Builder<AreaUtil.AreaNode>(this@MarketInfoAddActivity)
                    .setLinkedPicker(provinceList, cityList, districtList)
                    .setSelectOptions(
                        marketCity?.provinceId.orEmpty(),
                        marketCity?.cityId.orEmpty(),
                        marketCity?.areaId.orEmpty()
                    )
                    .setSubmitListener(object : OnOptionsSelectedListener<AreaUtil.AreaNode> {
                        override fun onOptionsSelect(
                            options: OptionsFragment<AreaUtil.AreaNode>,
                            options1: Int?, options2: Int?, options3: Int?
                        ) {
                            val province = provinceList[options1.orEmpty()]
                            val city = cityList[options1.orEmpty()][options2.orEmpty()]
                            val area = districtList[options1.orEmpty()][options2.orEmpty()][options3.orEmpty()]
                            extentParams.clear()
                            extentParams.add("province" to province.areaName)
                            extentParams.add("city" to city.areaName)
                            extentParams.add("area" to area.areaName)
                            contentText = city.areaName + area.areaName
                            marketCity = PreferenceArea(options1, options2, options3, province.areaName, city.areaName, area.areaName)
                            preferenceSave(marketCity, PreferenceConstants.MARKET_CITY)
                        }
                    })
                    .show()
            }
        }
    }

}